package com.mylg.craw.house.command;

import com.alibaba.excel.metadata.data.ReadCellData;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Getter
@Setter
public class OneSheet {
    private String month;

    private String sheetName;

    private String type = null;

    private Table<String, String, String> regionDetail = HashBasedTable.create();

    private int firstColIndex = 0;

    private Map<Integer, String> prefixIndex;

    private Map<Integer, String> titleIndex;

    private static final Map<String, String> correctValue = new HashMap<>();

    static {
        correctValue.put("住宝", "住宅");
        correctValue.put("非住宝", "非住宅");
        correctValue.put("问数", "间数");
        correctValue.put("3住宅", "非住宅");
        correctValue.put("丰住宝", "非住宅");
        correctValue.put("3E住宅", "非住宅");
        correctValue.put("3三住宅", "非住宅");
        correctValue.put("3非住宅", "非住宅");
    }

    public boolean parseHead(Map<Integer, ? extends Object> heads) {
        TreeMap<Integer, String> plain = new TreeMap<>();
        for (Map.Entry<Integer, ? extends Object> entry : heads.entrySet()) {
            if (entry.getValue() == null) {
                continue;
            }

            Object value = entry.getValue();

            if (value instanceof ReadCellData) {
                value = ((ReadCellData<?>) value).getStringValue();
                if (value == null) {
                    continue;
                }
            }

            String res = value.toString().trim();
            if (res.equals("行政区")) {
                firstColIndex = entry.getKey();
                continue;
            } else if (res.contains("商品房")) {
                type = "商品房";
            } else if (res.contains("二手房")) {
                type = "二手房";
            }
            plain.put(entry.getKey(), value.toString());
        }
        //没有头,直接忽略
        if (plain.isEmpty()) {
            return false;
        }

        if (plain.size() == 1) {
            return false;
        }

        if (prefixIndex == null) {
            prefixIndex = fillHead(plain);
            return false;
        } else {
            titleIndex = fillHead(plain);
            return true;
        }
    }

    private Map<Integer, String> fillHead(Map<Integer, String> plain) {
        Map<Integer, String> res = new HashMap<>();
        String before = "";
        for (int index = firstColIndex + 1; index < 7; index++) {
            before = plain.getOrDefault(index, before);
            before = correctValue.getOrDefault(before, before);
            res.put(index, before);
        }
        return res;
    }


    public void addRow(Map<Integer, String> content) {
        String row = content.get(0);
        if (row.endsWith("。") || row.endsWith("+")) {
            row = row.substring(0, row.length() - 1);
        }

        if (row.equals("二十区")) {
            row = "二七区";
        }
        for (Map.Entry<Integer, String> entry : content.entrySet()) {
            int index = entry.getKey();
            if (index == 0 || entry.getValue() == null) {
                continue;
            }
            String col = prefixIndex.get(index) + "-" + titleIndex.get(index);
            regionDetail.put(row, col, entry.getValue());
        }
    }
}
